package com.ruoyi.web.controller.wq;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wq.domain.WqActivity;
import com.ruoyi.wq.domain.WqTuanHome;
import com.ruoyi.wq.domain.vo.WqActivityVO;
import com.ruoyi.wq.domain.vo.WqTuanHomeVO;
import com.ruoyi.wq.service.IWqTuanHomeService;
import com.ruoyi.wq.service.IWqUserTuanService;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TuanServcie;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 拼团详情Controller
 * 
 * @author ruoyi
 * @date 2023-08-28
 */
@RestController
@RequestMapping("/wq/tuanHome")
public class WqTuanHomeController extends BaseController {

    @Autowired
    private IWqTuanHomeService wqTuanHomeService;
    
    @Autowired
    private IWqUserTuanService wqUserTuanService;
    
    @Autowired
    private TuanServcie tuanServcie;

    /**
     * 查询拼团列表
     */
    @Anonymous
    @GetMapping("/getList")
    public TableDataInfo getList(WqTuanHome wqTuanHome){
        TableDataInfo info = wqTuanHomeService.getPageList(wqTuanHome);
        return info;
    }

    /**
     * 导出拼团详情列表
     */
    @PreAuthorize("@ss.hasPermi('wq:tuanHome:export')")
    @Log(title = "拼团详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WqTuanHome wqTuanHome)
    {
        List<WqTuanHome> list = wqTuanHomeService.selectWqTuanHomeList(wqTuanHome);
        ExcelUtil<WqTuanHome> util = new ExcelUtil<WqTuanHome>(WqTuanHome.class);
        util.exportExcel(response, list, "拼团详情数据");
    }

    /**
     * 获取拼团详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('wq:tuanHome:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(wqTuanHomeService.selectWqTuanHomeById(id));
    }

    /**
     * 发起拼团
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WqTuanHome wqTuanHome){
        return AjaxResult.success(tuanServcie.saveTuanHome(wqTuanHome));
    }

    /**
     * 获取活动详细信息
     */
    @Anonymous
    @GetMapping(value = "/getDetail")
    public AjaxResult getDetail(String id){
    	AjaxResult ajax = AjaxResult.success();
    	WqTuanHome tuanHome = wqTuanHomeService.getById(id);
    	WqTuanHomeVO vo = new WqTuanHomeVO();
    	BeanUtils.copyProperties(tuanHome, vo, WqTuanHomeVO.class);
    	
    	long time = DateUtil.between(new Date(), vo.getTuanTime(), DateUnit.SECOND);
		vo.setTime(time);
		vo.setChaNum(vo.getUserNum()-vo.getPayNum());
    	List<SysUser> userList = wqUserTuanService.getUserListByTuanHomeId(id);
    	ajax.put("info", vo);
    	ajax.put("userList", userList);
        return ajax;
    }

    /**
     * 删除拼团详情
     */
    @PreAuthorize("@ss.hasPermi('wq:tuanHome:remove')")
    @Log(title = "拼团详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(wqTuanHomeService.deleteWqTuanHomeByIds(ids));
    }
}
